package org.bit.lesson06;

public class WaitNotifyExample {
    private static final Object lock = new Object();
    private static boolean isReady = false;

    public static void main(String[] args) {
        Thread waiter = new Thread(() -> {
            synchronized (lock) {
                while (!isReady) {
                    try {
                        System.out.println("等待线程进入等待状态...");
                        lock.wait(); // 释放锁并等待
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }

                }
                System.out.println("等待线程被唤醒，继续执行");
            }
        });

        Thread notifier = new Thread(() -> {
            synchronized (lock) {
                System.out.println("通知线程获得锁，准备通知");
                isReady = true;
                lock.notify(); // 唤醒一个等待线程
                System.out.println("已发送通知");
            }
        });

        waiter.start();
        try {
            Thread.sleep(1000); // 确保等待线程先执行
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        notifier.start();
    }
}
